<?php

class AbstractAction {
	var $registry;
	var $repository = null;
	var $request = null;
	var $smarty = null;
	var $errors = null;
	var $messages = null;
	var $lingvo = null;
	var $language = null;
	var $protectedPage = true;
	
	const ID_URL_LANG = "hl";
	const ID_SESS_LANG = "session_hl";
	
	const DEFF_LANG_ID = "en";
	
	public function __construct() {

		import('Language');
		$lang_id = null;
		if (isset($_GET[self::ID_URL_LANG])) $lang_id = $_SESSION[self::ID_SESS_LANG] = $_GET[self::ID_URL_LANG];
		if ($lang_id==null && isset($_SESSION[self::ID_SESS_LANG])) $lang_id = $_SESSION[self::ID_SESS_LANG];
		if ($lang_id==null) $lang_id = self::DEFF_LANG_ID;
		
		$this->language = new Language($lang_id);
    	
    	import("net.http.HttpRequest");
		import("net.http.HttpResponse");
		$this->request = new HttpRequest();
		$this->request->setMethod($_SERVER["REQUEST_METHOD"]);
		
		import('net.html.form.ActionErrors');
    	$this->errors = new ActionErrors();
    	$this->messages = new ActionErrors();
		
		import('smarty.Smarty');
	    $this->smarty=new Smarty;
	    $this->smarty->use_sub_dirs = false;
	    $this->smarty->template_dir = APPSERVER_ROOT. "/WEB-INF/templates";
	    $this->smarty->compile_dir  = APPSERVER_ROOT. "/WEB-INF/templates_c";

	    $this->smarty->assign_by_ref("language", $this->language);
	    $this->smarty->assign_by_ref("request", $this->request);
	    $this->smarty->assign_by_ref("errors", $this->errors);
	    $this->smarty->assign_by_ref("messages", $this->messages);
	    
	    $this->init();
	}
	
	
	/**
	* @return void
	* @desc Initialize controller
	*/
	function init() {
		
	}
	
	/**
	* @return void
	* @param Language $language
	* @desc Set current Language object
	*/
	function setLanguage($value) {
		$this->language = $value;
	}
	
	/**
	* @return Language
	* @desc Retrieve current Language object
	*/
	function getLanguage() {
		return $this->language;
	}
	
	
	function setProtectedPage($value) {
		$this->protectedPage = $value;
	}
	
	function isProtectedPage() {
		return $this->protectedPage;
	}
	
	
	function getAdditionalParams() {
		$route = $_GET["route"];
		if (isset($route) && strlen($route)>0) {
			return split("/",$route);
		}
		return array();
	}
	
	function execute($methodName) {
		if ($this->isProtectedPage()) {
			import('data.logic.user.User');
			if (!isset($_SESSION[User::KEY])) {
				$this->login();
				exit;
			}
		}
		$this->$methodName();
	}
	
	function login() {
		header("Location: /");
	}
	
	function index() {
		
	}
}
?>